package com.xiaoye.qbproject.config;

import com.alibaba.excel.util.StringUtils;
import com.xiaoye.qbproject.util.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 服务相关配置
 *
 * @author ruoyi
 */
@Slf4j
@Component
public class ServerConfig {
    @Value("${xy.uploadUrlPrefix}")
    private String uploadUrlPrefix;

    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getUrl() {
        if (StringUtils.isNotBlank(uploadUrlPrefix)) {
            log.info("自定义访问地址_uploadUrlPrefix:{}", uploadUrlPrefix);
            return uploadUrlPrefix;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
    }
}
